import 'package:distribution_mall_flutter/entity/category.dart';
import 'package:distribution_mall_flutter/http/modules/category.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class CategoryState {
  List<Category>? categoryList;
  CategoryState({this.categoryList});

  CategoryState.initial() : categoryList = [];

  CategoryState copyWith({
    List<Category>? categoryList,
  }) =>
      CategoryState(
        categoryList: categoryList ?? this.categoryList,
      );
}

final categoryProvider =
    StateNotifierProvider.autoDispose<CategoryViewModel, CategoryState>((ref) {
  return CategoryViewModel();
});

class CategoryViewModel extends StateNotifier<CategoryState> {
  CategoryViewModel() : super(CategoryState.initial());

  Future getCategoryList([int index = 0]) async {
    List<Category> categoryList = await CategoryApi.querySubCategory({});
    state = state.copyWith(categoryList: categoryList);
  }
}
